package com.example.pm.ending.server.service.ec.ownermodule;

import com.example.pm.ending.server.ex.ServiceException;
import com.example.pm.ending.server.mapper.*;
import com.example.pm.ending.server.module.owner.OwnerUpdateModuleDTO;
import com.example.pm.ending.server.pojo.entity.*;
import com.example.pm.ending.server.pojo.vo.*;
import com.example.pm.ending.server.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import static com.example.pm.ending.server.service.IOwnerModuleService.*;

@Component
@Slf4j
public class UpdateOwnerES {

    @Autowired
    OwnerMapper ownerMapper;
    @Autowired
    PublishOwnerMapper publishOwnerMapper;
    @Autowired
    HouseBuildMapper houseBuildMapper;
    @Autowired
    HouseUnitMapper houseUnitMapper;
    @Autowired
    BuildUnitRelMapper buildUnitRelMapper;
    @Autowired
    CarportMapper carportMapper;
    @Autowired
    CarportAreaMapper carportAreaMapper;
    @Autowired
    HouseMapper houseMapper;
    @Autowired
    ServeMapper serveMapper;


    //启用禁用模块
    public void setOwnerEnableState(Long ownerId, Integer enableState) {
        log.debug("开始处理[" + OWNER_ENABLE_STATE[enableState] + "业主]业务");
        //启用禁用 只能限制用户登录 不应修改相关信息
        ownerService(ownerId, enableState);
        log.debug("业主" + OWNER_ENABLE_STATE[enableState] + "完成");
    }

    private void ownerService(Long ownerId, Integer enableState) {

        log.debug("修改的业主id为:{},修改的状态为：{}", ownerId, enableState);
        OwnerStandardVO ownerSVO = ownerMapper.getOwnerById(ownerId);
        //
        if (ownerSVO == null) {
            String message = OWNER_ENABLE_STATE[enableState] + "失败,业主不存在,请刷新当前页面";
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        if (ownerSVO.getEnable().equals(enableState)) {
            String message = "该业主当前的状态已经是" + OWNER_ENABLE_STATE[enableState] + "状态,请刷新当前页面";
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }
        Owner owner = new Owner();
        owner.setId(ownerId);
        owner.setEnable(enableState);
        ownerMapper.update(owner);
        log.debug("owner表处理完毕");
    }

    /*启用禁用 是否处理 公布表？
    private void publishOwner(Long ownerId,Integer enableState){
        log.debug("开始处理publish_owner表信息");
        int countOwnerId = publishOwnerMapper.countByOwnerId(ownerId);
        if(countOwnerId == 0){
            log.debug("该用户未发布过信息,不进行结束");
            return;
        }
        PublishOwner publishOwner = new PublishOwner();
        publishOwner.setBrandEnable(enableState);
        publishOwner.setContentEnable(enableState);
        publishOwnerMapper.update(publishOwner);
        log.debug("完成了处理publish_owner表业务信息");
    }*/

    /*
    private Long ownerId;

    private String ownerName;

    private String ownerPhone;

    private String ownerIdCard;

    private String ownerGender;

    private Long houseBuildId;

    private Long houseUnitId;

    private String roomNum;

    private String carPartNumber;*/


    //用户信息修改
    public void updateOwnerInfo(OwnerUpdateModuleDTO dto) {
        ownerService(dto);


        buildService(dto.getHouseBuildId());

        unitService(dto.getHouseUnitId());

        BuildUnitRelStandardVO buildUnitRelStandardVO = buildUnitRel(dto.getHouseBuildId(), dto.getHouseUnitId());

        houseService(buildUnitRelStandardVO.getId(),dto,dto.getBeforeUpdateHouseId());

        carPortAreaService(dto.getCarPartAreaId());

        carPortService(dto,dto.getBeforeUpdateCarPartAreaId(),dto.getBeforeUpdateCarPortNumber());

    }

    /*处理owner表*/
    private void ownerService(OwnerUpdateModuleDTO dto) {
        OwnerStandardVO ownerStandardVO = ownerMapper.getOwnerById(dto.getOwnerId());
        if (ownerStandardVO == null) {
            String message = "修改失败,该用户不存在,刷新试试";
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        Integer idCardCount = ownerMapper.countByIdCardAndNotId(dto.getOwnerId(), dto.getOwnerIdCard());
        if (idCardCount != 0) {
            String message = "修改失败,身份证信息重复";
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }
        Integer phoneCount = ownerMapper.countByPhoneAndNotId(dto.getOwnerId(), dto.getOwnerPhone());
        if (phoneCount != 0) {
            String message = "修改失败,手机号重复";
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }
        Owner owner = new Owner();
        owner.setId(dto.getOwnerId());
        owner.setGender(dto.getOwnerGender());
        owner.setPhone(dto.getOwnerPhone());
        owner.setName(dto.getOwnerName());
        owner.setIdCard(dto.getOwnerIdCard());
        ownerMapper.update(owner);
    }

    private void buildService(Long buildId) {
        HouseBuildStandardVO houseBuildStandardVO = houseBuildMapper.getHouseBuildById(buildId);
        if (houseBuildStandardVO == null) {
            String message = "修改失败,不存在此房区(栋)";
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
    }

    private void unitService(Long unitId) {
        HouseUnit houseUnitStandardVO = houseUnitMapper.getHouseUnitById(unitId);
        if (houseUnitStandardVO == null) {
            String message = "修改失败,该区(栋)单元不存在,刷新试试";
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
    }

    private BuildUnitRelStandardVO buildUnitRel(Long buildId, Long unitId) {

        BuildUnitRelStandardVO buildUnitRelStandardVO = buildUnitRelMapper.getStandardByBuildIdAndUnitId(buildId, unitId);
        if (buildUnitRelStandardVO == null) {
            String message = "数据异常";
            throw new ServiceException(ServiceCode.ERROR_BAD_REQUEST, message);
        }
        return buildUnitRelStandardVO;
    }

    private void houseService(Long buildUnitRelId, OwnerUpdateModuleDTO dto,Long beforeUpdateHouseId) {
        //修改业主房产应将之前的房产 所属业主清除,并将房屋状态改为空闲,不需要服务状态
        House house = new House();
        HouseStandardVO houseBeforeUpdate = houseMapper.getHouseById(beforeUpdateHouseId);
        houseBeforeUpdate.setHouseStateId(HOUSE_STATE_IDLE);
        houseBeforeUpdate.setOwnerId(null);
        houseBeforeUpdate.setCode(HOUSE_CODE_NO);
        BeanUtils.copyProperties(houseBeforeUpdate,house);
        houseMapper.update(house);

        HouseStandardVO houseStandardUpVO = houseMapper.getHouseByRoomNumAndBuildUnitRelId(dto.getRoomNum(), buildUnitRelId);
        if (houseStandardUpVO == null) {
            String message = "数据异常";
            throw new ServiceException(ServiceCode.ERROR_UPDATE,message);
        }
        if(houseStandardUpVO.getOwnerId() != null){
            String message ="修改失败,该房屋已有其他业主使用";
            throw new ServiceException(ServiceCode.ERROR_CONFLICT,message);
        }
        House houseUpdate = new House();
        houseUpdate.setHouseStateId(HOUSE_STATE_SOLD);
        houseUpdate.setOwnerId(dto.getOwnerId());
        houseUpdate.setId(houseStandardUpVO.getId());
        houseUpdate.setCode(HOUSE_CODE_NO);
        houseMapper.update(house);
    }

    private void carPortAreaService(Long carPartAreaId){
        if(carPartAreaId == null){
            return;
        }
        CarportAreaStandardVO carportAreaStandardVO = carportAreaMapper.getStandardById(carPartAreaId);
        if (carportAreaStandardVO == null) {
            String message = "修改失败,该车区不存在,刷新试试";
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
    }

    private void carPortService(OwnerUpdateModuleDTO dto,Long beforeUpdateCarPartAreaId,String beforeUpdateCarPortNumber){
        if(dto.getCarPartNumber()==null && dto.getCarPartAreaId()==null){
            Carport carportBeforeInfo = carportMapper.getCarportByAreaIdAndNumber(beforeUpdateCarPartAreaId, beforeUpdateCarPortNumber);
            carportBeforeInfo.setOwnerId(null);
            carportMapper.update(carportBeforeInfo);
            return;
        }
        if(dto.getCarPartAreaId()==null){
            String message = "修改失败,数据异常";
            throw new ServiceException(ServiceCode.ERROR_BAD_REQUEST,message);
        }
        if(dto.getCarPartNumber()==null){
            String message = "修改失败,数据异常";
            throw new ServiceException(ServiceCode.ERROR_BAD_REQUEST,message);
        }
        Carport carportByAreaIdAndNumber = carportMapper.getCarportByAreaIdAndNumber(dto.getCarPartAreaId(), dto.getCarPartNumber());
        if(carportByAreaIdAndNumber == null){
            String message ="修改失败，修改的车库数据不存在";
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,message);
        }
        if(carportByAreaIdAndNumber.getOwnerId()!=null){
            String message = "修改失败,修改的车库已被其他业主使用";
            throw new ServiceException(ServiceCode.ERROR_CONFLICT,message);
        }
        Carport carportBeforeInfo = carportMapper.getCarportByAreaIdAndNumber(beforeUpdateCarPartAreaId, beforeUpdateCarPortNumber);
        carportBeforeInfo.setOwnerId(null);
        carportBeforeInfo.setId(carportBeforeInfo.getId());
        carportMapper.update(carportBeforeInfo);


        Carport carport = new Carport();
        carport.setOwnerId(dto.getOwnerId());
        carport.setId(carportByAreaIdAndNumber.getId());
        carportMapper.update(carport);
    }


}
